import os
import time
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import QApplication, QMainWindow, QIcon, QPixmap, QFileDialog, QWidget, QDesktopWidget, QMessageBox, \
    QLabel
from PyQt5.QtCore import Qt, QFileInfo, QFile

from config import Cfg
from utils.new_window import NewWindow


class PageWin(QMainWindow):
    def __init__(self):
        super(PageWin, self).__init__()

        desktop = QApplication.desktop()
        screen_rect = desktop.screenGeometry()
        height = screen_rect.height()
        width = screen_rect.width()

        self.h_ratio = height / Cfg.dic['winh']
        self.w_ratio = width / Cfg.dic['winw']

        self.setWindowIcon(QIcon(':images/logo.png'))

    def setup_ui(self, mainwin):
        self.window = mainwin
        self.window.resize(
            int(self.w_ratio * 800),
            int(self.h_ratio * 600)
        )

        # 背景图片
        window_pale = QtGui.QPalette()
        window_pale.setBrush(
            self.backgroundRole(),
            QtGui.QBrush(
                QtGui.QPixmap(':images/background.png').scaled(
                    int(self.w_ratio * 800),
                    int(self.h_ratio * 600)
                )
            )
        )
        self.window.setPalette(window_pale)

        # 插板
        self.centralWidget = QtWidgets.QWidget(self.window)
        self.centralWidget.resize(
            int(self.w_ratio * 800),
            int(self.h_ratio * 600)
        )

        # 同一窗口下多页面（会话窗口）切换
        self.stackedWidget = QtWidgets.QStackedWidget(self.centralWidget)
        self.stackedWidget.setGeometry(
            QtCore.QRect(
                int(self.w_ratio * 0),
                int(self.h_ratio * 0),
                int(self.w_ratio * 800),
                int(self.h_ratio * 800)
            )
        )
        self.stackedWidget.setObjectName('多页面切换')

        ##################################################
        #                   第一个页面                    #
        ##################################################

        self.page1 = QtWidgets.QWidget()
        self.page1.setObjectName('第一个页面')

        # 最上部分
        self.groupBox = QtWidgets.QGroupBox(self.page1)
        self.groupBox.setObjectName('groupBox')
        self.groupBox.setGeometry(QtCore.QRect(50, 100, 1000, 120))

        # DNA大分子label
        self.DNA_label = QtWidgets.QLabel(self.groupBox)
        self.DNA_label.setGeometry(QtCore.QRect(80, 20, 150, 40))
        self.DNA_label.setObjectName('DNA')
        self.DNA_label.setStyleSheet(
            'QLabel{font-size:20px; text-align:center;}'
        )

        # DNAlabel右边的矩形框
        self.openDNA_edit = QtWidgets.QLineEdit(self.groupBox)
        self.openDNA_edit.setGeometry(QtCore.QRect(250, 20, 500, 40))
        self.openDNA_edit.setObjectName('openDNA_edit')
        self.openDNA_edit.setReadOnly(True)

        # 打开DNA文件
        self.openDNA_btn = QtWidgets.QPushButton(self.groupBox)
        self.openDNA_btn.setGeometry(QtCore.QRect(750, 20, 150, 40))
        self.openDNA_btn.setObjectName('openDNA')
        self.openDNA_btn.clicked.connect(self.openDNA_func)
        self.openDNA_btn.setStyleSheet(
            'QPushButton{font-size:20px; text-align:center;}'
        )

        # 抗生素小分子label
        self.antib_label = QtWidgets.QLabel(self.groupBox)
        self.antib_label.setGeometry(QtCore.QRect(80, 60, 150, 40))
        self.antib_label.setObjectName('antibiotic')
        self.antib_label.setStyleSheet(
            'QLabel{font-size:20px; text-align:center;}'
        )

        # 抗生素label右边的矩形框
        self.openAntib_edit = QtWidgets.QLineEdit(self.groupBox)
        self.openAntib_edit.setGeometry(QtCore.QRect(250, 60, 500, 40))
        self.openAntib_edit.setObjectName('openAntib_edit')
        self.openAntib_edit.setReadOnly(True)

        # 打开抗生素文件
        self.openAntib_btn = QtWidgets.QPushButton(self.groupBox)
        self.openAntib_btn.setGeometry(QtCore.QRect(750, 60, 150, 40))
        self.openAntib_btn.setObjectName('openAntib')
        self.openAntib_btn.clicked.connect(self.openAntib_func)
        self.openAntib_btn.setStyleSheet(
            'QPushButton{font-size:20px; text-align:center;}'
        )

        # 中间部分
        self.groupBox2 = QtWidgets.QGroupBox(self.page1)
        self.groupBox2.setObjectName('groupBox2')
        self.groupBox2.setGeometry(QtCore.QRect(50, 250, 1000, 400))

        # DNA进度条
        self.DNA_bar = QtWidgets.QProgressBar(self.groupBox2)
        self.DNA_bar.setGeometry(QtCore.QRect(80, 35, 360, 300))
        self.DNA_bar.setTextVisible(False)
        self.DNA_bar.setObjectName('DNA进度条')
        self.DNA_bar.setStyleSheet(
            'border:0px;'
        )

        self.groupBoxDNA = QtWidgets.QGroupBox(self.groupBox2)
        self.groupBoxDNA.setObjectName('DNA展示框')
        self.groupBoxDNA.setGeometry(QtCore.QRect(80, 50, 360, 300))

        self.DNA_view = QtWidgets.QLabel(self.groupBoxDNA)
        self.DNA_view.setGeometry(QtCore.QRect(0, 0, 360, 300))
        self.DNA_view.setObjectName('DNA展示区域')
        self.DNA_view.setPixmap(QPixmap(':images/picture.png'))
        self.DNA_view.setStyleSheet('background:white; border:1px solid #cdcdcd;')
        self.DNA_view.setAlignment(Qt.AlignCenter)

        # 抗生素进度条
        self.antib_bar = QtWidgets.QProgressBar(self.groupBox2)
        self.antib_bar.setGeometry(QtCore.QRect(550, 35, 360, 300))
        self.antib_bar.setTextVisible(False)
        self.antib_bar.setObjectName('抗生素进度条')
        self.antib_bar.setStyleSheet(
            'border:0px;'
        )

        self.groupBoxAntib = QtWidgets.QGroupBox(self.groupBox2)
        self.groupBoxAntib.setObjectName('抗生素展示框')
        self.groupBoxAntib.setGeometry(QtCore.QRect(550, 50, 360, 300))

        self.Antib_view = QtWidgets.QLabel(self.groupBoxAntib)
        self.Antib_view.setGeometry(QtCore.QRect(0, 0, 360, 300))
        self.Antib_view.setObjectName('抗生素展示区域')
        self.Antib_view.setPixmap(QPixmap(':images/picture.png'))
        self.Antib_view.setStyleSheet('background:white; border:1px solid #cdcdcd;')
        self.Antib_view.setAlignment(Qt.AlignCenter)

        # 最下部分
        self.groupBox3 = QtWidgets.QGroupBox(self.page1)
        self.groupBox3.setObjectName('groupBox3')
        self.groupBox3.setGeometry(QtCore.QRect(50, 680, 1000, 100))

        self.next_btn = QtWidgets.QPushButton(self.groupBox3)
        self.next_btn.setObjectName('下一步')
        self.next_btn.setGeometry(QtCore.QRect(440, 30, 120, 40))
        self.next_btn.setStyleSheet(
            'QPushButton{font-size:20px; text-align:center;}'
        )
        self.next_btn.clicked.connect(self._next_func)

        self.stackedWidget.addWidget(self.page1)

        ##################################################
        #                   第二个页面                    #
        ##################################################

        self.page2 = QtWidgets.QWidget()
        self.page2.setObjectName('第二个页面')

        # 最上部分
        self.groupBox4 = QtWidgets.QGroupBox(self.page2)
        self.groupBox4.setObjectName('groupBox4')
        self.groupBox4.setGeometry(QtCore.QRect(50, 100, 1000, 120))

        # DNA标签
        self.DNA_label2 = QtWidgets.QLabel(self.groupBox4)
        self.DNA_label2.setGeometry(QtCore.QRect(80, 20, 150, 40))
        self.DNA_label2.setObjectName('DNA标签')
        self.DNA_label2.setStyleSheet(
            'QLabel{font-size:20px; text-align:center;}'
        )

        # DNA文件路径
        self.DNA_edit2 = QtWidgets.QLabel(self.groupBox4)
        self.DNA_edit2.setGeometry(QtCore.QRect(250, 20, 500, 40))
        self.DNA_edit2.setObjectName('DNA文件路径')
        self.DNA_edit2.setStyleSheet(
            'QLabel{border: 1px solid #ccc; font-size:20px; text-align:center;}'
        )

        # 抗生素标签
        self.antib_label2 = QtWidgets.QLabel(self.groupBox4)
        self.antib_label2.setGeometry(QtCore.QRect(80, 60, 150, 40))
        self.antib_label2.setObjectName('抗生素标签')
        self.antib_label2.setStyleSheet(
            'QLabel{font-size:20px; text-align:center;}'
        )

        # 抗生素文件路径
        self.antib_edit2 = QtWidgets.QLabel(self.groupBox4)
        self.antib_edit2.setGeometry(QtCore.QRect(250, 60, 500, 40))
        self.antib_edit2.setObjectName('抗生素文件路径')
        self.antib_edit2.setStyleSheet(
            'QLabel{border: 1px solid #ccc; font-size:20px; text-align:center;}'
        )

        # 混合分子按钮
        self.merge_btn = QtWidgets.QPushButton(self.groupBox4)
        self.merge_btn.setGeometry(QtCore.QRect(780, 30, 120, 60))
        self.merge_btn.setObjectName('混合两种分子')
        self.merge_btn.clicked.connect(self._merge_molecule)
        self.merge_btn.setStyleSheet(
            'QPushButton{font-size:20px; text-align:center;}'
        )

        # 中间部分
        self.groupBox5 = QtWidgets.QGroupBox(self.page2)
        self.groupBox5.setObjectName('groupBox5')
        self.groupBox5.setGeometry(QtCore.QRect(50, 250, 1000, 400))

        # 混合分子进度条
        self.merge_bar = QtWidgets.QProgressBar(self.groupBox5)
        self.merge_bar.setGeometry(QtCore.QRect(200, 15, 600, 15))
        self.merge_bar.setTextVisible(False)
        self.merge_bar.setObjectName('混合分子进度条')
        self.merge_bar.setStyleSheet(
            'border:0px;'
        )

        self.groupBoxMerge = QtWidgets.QGroupBox(self.groupBox5)
        self.groupBoxMerge.setObjectName('混合分子展示框')
        self.groupBoxMerge.setGeometry(QtCore.QRect(200, 30, 600, 350))

        self.molecule_view = QtWidgets.QLabel(self.groupBoxMerge)
        self.molecule_view.setGeometry(QtCore.QRect(0, 0, 600, 350))
        self.molecule_view.setObjectName('混合分子展示区域')
        self.molecule_view.setPixmap(QPixmap(':images/picture.png'))
        self.molecule_view.setStyleSheet('background:white; border:1px solid #cdcdcd;')
        self.molecule_view.setAlignment(Qt.AlignCenter)

        # 最下部分
        self.groupBox6 = QtWidgets.QGroupBox(self.page2)
        self.groupBox6.setObjectName('groupBox6')
        self.groupBox6.setGeometry(QtCore.QRect(50, 680, 1000, 100))

        self.pre_btn = QtWidgets.QPushButton(self.groupBox6)
        self.pre_btn.setObjectName('上一步')
        self.pre_btn.setGeometry(QtCore.QRect(350, 30, 120, 40))
        self.pre_btn.setStyleSheet(
            'QPushButton{font-size:20px; text-align:center;}'
        )
        self.pre_btn.clicked.connect(self._pre_func)

        self.export_btn = QtWidgets.QPushButton(self.groupBox6)
        self.export_btn.setObjectName('导出')
        self.export_btn.setGeometry(QtCore.QRect(500, 30, 120, 40))
        self.export_btn.setStyleSheet(
            'QPushButton{font-size:20px; text-align:center;}'
        )
        self.export_btn.clicked.connect(self._export_func)

        self.stackedWidget.addWidget(self.page2)

        ##################################################
        #                    其他功能                     #
        ##################################################

        # 最小化
        self.mini_btn = QtWidgets.QPushButton(self.centralWidget)
        self.mini_btn.setObjectName('最小化')
        self.mini_btn.setGeometry(
            QtCore.QRect(
                int(self.w_ratio * 720),
                0,
                int(self.w_ratio * 30),
                int(self.h_ratio * 30)
            )
        )
        self.mini_btn.setStyleSheet(
            'QPushButton{border-image:url(:images/mini.png);};'
            'QPushButton:hover{border-image:url(:images/mini_1.png);};'
        )
        self.mini_btn.clicked.connect(self._mini)

        # 关闭
        self.out_btn = QtWidgets.QPushButton(self.centralWidget)
        self.out_btn.setObjectName('退出')
        self.out_btn.setGeometry(
            QtCore.QRect(
                int(self.w_ratio * 760),
                0,
                int(self.w_ratio * 30),
                int(self.h_ratio * 30)
            )
        )
        self.out_btn.setStyleSheet(
            'QPushButton{border-image: url(:images/out.png);};'
            'QPushButton:hover{border-image: url(:images/out_1.png);};'
        )
        self.out_btn.clicked.connect(self._out)

        self.retranslate_ui(self.window)

    # 渲染
    def retranslate_ui(self, mainwin):
        _translate = QtCore.QCoreApplication.translate
        mainwin.setWindowTitle(_translate('mainwin', 'mainwin'))

        # page1
        self.DNA_label.setText(_translate('mainwin', 'DNA大分子'))
        self.antib_label.setText(_translate('mainwin', '抗生素小分子'))
        self.openDNA_btn.setText(_translate('mainwin', '选择文件'))
        self.openAntib_btn.setText(_translate('mainwin', '选择文件'))
        self.next_btn.setText(_translate('mainwin', '下一步'))

        # page2
        self.DNA_label2.setText(_translate('mainwin', 'DNA大分子'))
        self.antib_label2.setText(_translate('mainwin', '抗生素小分子'))
        self.merge_btn.setText(_translate('mainwin', '混合分子'))
        self.pre_btn.setText(_translate('mainwin', '上一步'))
        self.export_btn.setText(_translate('mainwin', '导出'))

    def _out(self):
        self.window.close()

    def _mini(self):
        self.window.showMinimized()

    def openDNA_func(self):
        '''
        打开DNA文件
        '''
        dir, filetype = QFileDialog.getOpenFileName(self, '选择文件', 'E:/', 'pdb file (*.pdb, *)')
        self.openDNA_edit.setText(''.join(dir))
        Cfg.dic['openfile_DNA'] = dir

        if self.openDNA_edit.text().strip():
            print('已选择文件', dir)
            # 进度条
            self.DNA_bar.setProperty('value', '99')

            # 加载模型，在view中展示
            p = QPixmap(':images/DNA.png')
            self.DNA_view.setPixmap(p.scaled(360, 300))
            # 进度条结束
            time.sleep(3)
            self.DNA_bar.setProperty('value', 0)

    def openAntib_func(self):
        '''
        打开抗生素文件
        '''
        dir, filetype = QFileDialog.getOpenFileName(self, '选择文件', 'E:/', 'pdb file (*.pdb, *)')
        self.openAntib_edit.setText(''.join(dir))
        Cfg.dic['openfile_antib'] = dir

        if self.openAntib_edit.text().strip():
            print('已选择文件', dir)
            # 进度条
            self.antib_bar.setProperty('value', '99')

            # 加载模型，在view中展示
            p = QPixmap(':images/antibiotic.png')
            self.Antib_view.setPixmap(p.scaled(360, 300))
            # 进度条结束
            time.sleep(3)
            self.antib_bar.setProperty('value', 0)

    def _next_func(self):
        '''
        下一页
        '''
        if self.openDNA_edit.text().strip() and self.openAntib_edit.text().strip():
            self.DNA_edit2.setText(Cfg.dic['openfile_DNA'])
            self.antib_edit2.setText(Cfg.dic['openfile_antib'])
            self.stackedWidget.setCurrentIndex(1)
        else:
            QMessageBox.warning(self, '警告', '请选择文件', QMessageBox.Close)

    def _merge_molecule(self):
        '''
        混合DNA分子与抗生素分子
        '''
        if self.DNA_edit2.text().strip() and self.antib_edit2.text().strip():
            print('merging ...')

            # 进度条
            self.merge_bar.setProperty('value', '99')

            # 加载模型，在view中展示
            p = QPixmap(':images/DNA.png')
            self.molecule_view.setPixmap(p.scaled(600, 350))
            # 进度条结束
            time.sleep(3)
            self.merge_bar.setProperty('value', 0)

    def _pre_func(self):
        '''
        上一页
        '''
        self.stackedWidget.setCurrentIndex(0)

    def _export_func(self):
        '''
        导出文件
        '''


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    win = PageWin()
    win.setup_ui(win)
    win.show()
    app.exit(app.exec_())
